#! /usr/bin/perl -w

use Expect;
use strict;

my $timeout = 10;
my $username = "test";
my $password = "test";

my $exp = Expect->spawn("ssh", "$username\@localhost");
#$exp->log_stdout(0);
#$exp->debug(1);

$exp->expect($timeout,
               [ qr/password/i => sub { my $exp = shift;
                                        $exp->send("$password\n");
                                      } ],
            );
$exp->expect($timeout,
             [ qr/\$/ => sub { $exp->send("pwd\n"); }]
            );
$exp->expect($timeout,
             [ qr/\$/ => \&do_ls ]
            );
$exp->expect($timeout,
             [ qr/\$/ => \&do_bash_cmd ]
            );
$exp->send("exit\n");
$exp->soft_close;

sub do_ls() {
    my $e = shift;
    $e->send("ls\n");
}

sub do_bash_cmd() {
    my $e = shift;
    $e->send("[[ -e test.sh ]] && ./test.sh\n");
}
